﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace Timer
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private System.Timers.Timer timer;

        public MainWindow()
        {
            InitializeComponent();

            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.AutoReset = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            hour.Focus();

            EndMessageTextEdit.EndMessage = "Timer ist abgelaufen!";

        }
        public string focusedTextBox;
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                //sec.Text = Convert.ToString(Convert.ToInt32(sec.Text) - 1);

                if (sec.Text.Equals("0"))
                {
                    if (!min.Text.Equals("0"))
                    {
                        min.Text = Convert.ToString(Convert.ToInt32(min.Text) - 1);
                        sec.Text = "60";
                    }
                    else
                    {
                        if (!hour.Text.Equals("0"))
                        {
                            hour.Text = Convert.ToString(Convert.ToInt32(hour.Text) - 1);
                            min.Text = "59";
                            sec.Text = "60";
                        }
                    }
                }
                if (sec.Text.Equals("0") && min.Text.Equals("0") && hour.Text.Equals("0"))
                {
                    stopEvent();
                    if (Shutdown_Check.IsChecked==true)
                    {
                        shutdown();
                    }
                    else
                    {
                        var timerEndScreen = new TimerEndScreen();
                        timerEndScreen.ShowDialog();
                    }
                    
                }
                else
                {
                    sec.Text = Convert.ToString(Convert.ToInt32(sec.Text) - 1);
                }
            }));


        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            stop_button.IsEnabled = false;
        }

        private void start_button_Click(object sender, RoutedEventArgs e)
        {
            start_button.IsEnabled = false;
            stop_button.IsEnabled = true;
            sec.IsEnabled = false;
            min.IsEnabled = false;
            hour.IsEnabled = false;
            timeMin.IsEnabled = false;
            timeHour.IsEnabled = false;

            if ((timeHour.Text.Length != 0) && (timeMin.Text.Length != 0))
            {
                hour.Clear();
                min.Clear();
                sec.Clear();




                int eMin = Convert.ToInt32(timeMin.Text);
                int eHour = Convert.ToInt32(timeHour.Text);
                DateTime t = System.DateTime.Now;
                int ergSec = 0;
                if ((eHour < t.Hour || eHour <= t.Hour) && (eMin < t.Minute || eMin <= t.Minute))
                {
                    //Aktuelle Uhrzeit - Angegebene Uhrzeit
                    ergSec = 86400 - ((((t.Hour * 60) * 60) + (t.Minute * 60)) - (((eHour * 60) * 60) + (eMin * 60)));
                }
                else
                {
                    //Angegebene Uhrzeit - Aktuelle Uhrzeit
                    ergSec = ((((eHour * 60) * 60) + (eMin * 60)) - (((t.Hour * 60) * 60) + (t.Minute * 60)));
                }

                if (Convert.ToInt32(timeHour.Text) > 24 || Convert.ToInt32(timeHour.Text) < 0 || Convert.ToInt32(timeMin.Text) > 60 || Convert.ToInt32(timeMin.Text) < 0)
                {
                    MessageBox.Show("Ungültige Einträge", "Fehler");
                    stopEvent();
                }
                else
                {
                    sec.Text = ergSec.ToString();
                    zeitUmwandeln();

                    ergSec = 0;
                    eMin = 0;
                    eHour = 0;


                    timer.Start();
                }
            }
            else
            {
                zeitUmwandeln();
                timer.Start();
            }
        }

        private void stop_button_Click(object sender, RoutedEventArgs e)
        {
            stopEvent();
        }

        private void zeitUmwandeln()
        {
            if (sec.Text.Length == 0)
            {
                sec.Text = "0";
            }
            if (min.Text.Length == 0)
            {
                min.Text = "0";
            }
            if (hour.Text.Length == 0)
            {
                hour.Text = "0";
            }

            while (Convert.ToInt32(sec.Text) > 60 || Convert.ToInt32(min.Text) > 60)
            {
                if ((Convert.ToInt32(sec.Text)) > 60)
                {
                    min.Text = Convert.ToString(Convert.ToInt32(min.Text) + 1);
                    sec.Text = Convert.ToString(Convert.ToInt32(sec.Text) - 60);
                }
                if ((Convert.ToInt32(min.Text)) > 60)
                {
                    hour.Text = Convert.ToString(Convert.ToInt32(hour.Text) + 1);
                    min.Text = Convert.ToString(Convert.ToInt32(min.Text) - 60);
                }
            }

        }

        private void stopEvent()
        {
            stop_button.IsEnabled = false;
            start_button.IsEnabled = true;
            timeHour.Clear();
            timeMin.Clear();
            sec.IsEnabled = true;
            min.IsEnabled = true;
            hour.IsEnabled = true;
            timeHour.IsEnabled = true;
            timeMin.IsEnabled = true;
            timer.Stop();
        }

        public static bool onlyNumeric(string text)
        {
            Regex regex = new Regex("[^0-9.-]+"); //regex that allows numeric input only
            return !regex.IsMatch(text); // 
        }

        private void WindowOpened_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.NumPad1:
                case Key.D1:
                case Key.NumPad2:
                case Key.D2:
                case Key.NumPad3:
                case Key.D3:
                case Key.NumPad4:
                case Key.D4:
                case Key.NumPad5:
                case Key.D5:
                case Key.NumPad6:
                case Key.D6:
                case Key.NumPad7:
                case Key.D7:
                case Key.NumPad8:
                case Key.D8:
                case Key.NumPad9:
                case Key.D9:
                case Key.NumPad0:
                case Key.D0:
                case Key.Back:
                    break;

                case Key.Return:
                    start_button_Click(null, null);
                    break;

                default: break;

            }
        }

        private void shutdown()
        {
            System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\shutdown.exe", "-s -t 0");
        }

        private void hour_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !onlyNumeric(e.Text);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            EndMessageTextEdit endMessageTextEdit = new EndMessageTextEdit();
            endMessageTextEdit.ShowDialog();
        }

    }
}
